import Vue from 'vue'
import Router from 'vue-router'

import Statement from './statement'
import Classify from './classify'

Vue.use(Router)

const router = new Router({
  base: '/financial/',
  routes: [
    {
      name: 'default',
      path: '',
      component: resolve => require(['@/components/login/Login'], resolve)
    },
    {
      name: 'login',
      path: '/login',
      component: resolve => require(['@/components/login/Login'], resolve)
    },
    {
      name: '首页',
      path: '/index',
      redirect: '/index/index',
      component: resolve => require(['@/views/Home'], resolve),
      children: [
        {
          name: '404',
          path: '404',
          component: resolve => require(['@/components/error/404'], resolve)
        }, {
          name: '500',
          path: '500',
          component: resolve => require(['@/components/error/500'], resolve)
        }, {
          name: '首页',
          path: 'index',
          component: resolve => require(['@/views/index/Index'], resolve)
        },
        ...Statement, ...Classify
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let userId = sessionStorage.getItem('userId')
    if (userId) {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

export default router
